package com.spsy.pay.controller.api;

import com.spsy.common.core.domain.R;
import com.spsy.domain.R2;
import com.spsy.pay.api.SpsyPayProvider;
import com.spsy.pay.service.PayApiService;
import com.spsy.pay.sign.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: liuzhiping
 * @date: 2024/12/17 15:41
 **/
@RestController
@Slf4j
public class PayFeignApiController implements SpsyPayProvider {

    @Autowired
    private PayApiService payApiService;
    @Override
    public R2 orderPay(String orderId,String sign) throws Exception {


        /***
         *订单系统验签
         Map<String,String> map = new HashMap<>();
         map.put("orderId",orderId);

         String sign = SignUtil.genSign(map,"3w24y82r4y23@","HMACSHA256");

         R2 r = this.spsyPayProvider.orderPay(orderId,sign);
         */

        orderId = "57727541-9f1f-4917-96ae-30476561b7089999999";

        log.info("orderId:{} sign:{}",orderId,sign);
        //验签逻辑
        //订单给签名sign 与 支付生成的签名sign做对比，如果相等则成功。否则返回失败。

        Map<String,String> map = new HashMap<>();

        map.put("orderId",orderId);
        String newSign = SignUtil.genSign(map,"3w24y82r4y23@","HMACSHA256");

        log.info("sign order:{}",sign);
        log.info("sign   pay:{}",newSign);



        //验签
        if(!sign.equals(newSign)){
            return R2.ok("签名失败");
        }

        return  R2.ok(this.payApiService.pay(orderId));
    }
}
